#ifndef JSTRINGLIST_H
#define JSTRINGLIST_H

#include <JXQCore/JList>
#include <JXQCore/JString>
#include <JXQCore/JDebug>
#include <memory>

namespace JXQ
{

template <typename TChar>
class JListBase<JBasicString<TChar>>
{
protected:
	JListBase() = default;
	virtual ~JListBase() = 0;

public:
	JBasicString<TChar> join(const JBasicString<TChar> &connection = "");
};

template <typename TChar>
class JBasicStringList : public JList<JBasicString<TChar>>
{
	using JList<JBasicString<TChar>>::JList;

public:
	std::shared_ptr<const char*> c_strV2() const;
};

template <typename TChar>
JListBase<JBasicString<TChar>>::~JListBase() {}

template <typename TChar>
JBasicString<TChar> JListBase<JBasicString<TChar>>::join(const JBasicString<TChar> &connection)
{
	auto pList = J_DCT(JBasicStringList<TChar>*, this);
	if( pList == nullptr )
		return JBasicString<TChar>();

	JBasicString<TChar> res;
	for(auto &str : *pList)
		res += str + connection;

	res.erase(res.size() - connection.size(), connection.size());
	return res;
}

template <typename TChar> // need delete[] char**
std::shared_ptr<const char*> JBasicStringList<TChar>::c_strV2() const
{
	const char **pv2 = new const char*[this->size() + 1];
	auto it = this->begin();
	int i = 0;

	for(; it!=this->end(); ++it, i++)
		pv2[i] = it->c_str();
	pv2[i] = nullptr;

	return std::shared_ptr<const char*>(pv2);
}

template <typename TChar>
inline JDebug &operator<<(JDebug &d, const JBasicStringList<TChar> *slist)
{
	return d << J_SCT(const JList<JBasicString<TChar>>*, slist);
}

template <typename TChar>
inline JDebug &operator<<(JDebug &&d, const JBasicStringList<TChar> *slist)
{
	return d << J_SCT(const JList<JBasicString<TChar>>*, slist);
}

template <typename TChar>
inline JDebug &operator<<(JDebug &d, const JBasicStringList<TChar> &slist)
{
	return d << J_SCT(const JList<JBasicString<TChar>>&, slist);
}

template <typename TChar>
inline JDebug &operator<<(JDebug &&d, const JBasicStringList<TChar> &slist)
{
	return d << J_SCT(const JList<JBasicString<TChar>>&, slist);
}

} // namespace JXQ


#endif //JSTRINGLIST_H
